//
//  CGroupMgr.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/5/22.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "CGroupMgr.h"
#import "FMDB.h"
#import "CFolderMgr.h"
#import "MicroDef.h"
#import "CGroupModel.h"
@implementation CGroupMgr


//插入群
-(BOOL)insertGroup:(CGroupModel *)group
{
    if(group == nil)
    {
        return NO;
    } else {
        if (M_ISSTRNULL(group.GroupId)) {
            return NO;
        }
    }
    __block BOOL ret = NO;

    NSString *sql = nil;
    sql = [NSString stringWithFormat:@"select * from GROUPINFO where  GroupId = \'%@\'", group.GroupId];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        if (result && [result next]) {
            ret = YES;
        }
        [result close];
        
        if (ret == YES) {
        
//                ret = [db executeUpdate:@"UPDATE GROUPINFO SET GroupIntroduction = ?, OwnerCode = ?, OwnerName = ?, UserNum = ?, CreateDate = ?, GroupName = ?, OwnerId = ?, GroupNotice = ?,messageSettingType = ?, PinYin = ?,IsDel = ?,isShow = ?,groupType = ? where GroupId = ? ", M_STOREVALUE(group.GroupIntroduction), M_STOREVALUE(group.OwnerCode), M_STOREVALUE(group.OwnerName), @(group.UserNum), M_STOREVALUE(group.GroupCreateDate),M_STOREVALUE(group.GroupName),M_STOREVALUE(group.OwnerId),M_STOREVALUE(group.GroupNotice),@(group.messageSettingType), M_STOREVALUE(group.PinYin),group.IsDel?@1:@0,group.isShow?@1:@0,@(group.groupType), M_STOREVALUE(group.GroupId)];
            //bug 8951 fixed by lixu
              ret = [db executeUpdate:@"UPDATE GROUPINFO SET GroupIntroduction = ?, OwnerCode = ?, OwnerName = ?, UserNum = ?, CreateDate = ?, GroupName = ?, OwnerId = ?, GroupNotice = ?, PinYin = ?,IsDel = ?,isShow = ?,groupType = ?,yuLiuZiduan2 = ?,yuLiuZiduan3 = ? where GroupId = ? ", M_STOREVALUE(group.GroupIntroduction), M_STOREVALUE(group.OwnerCode), M_STOREVALUE(group.OwnerName), @(group.UserNum), M_STOREVALUE(group.GroupCreateDate),M_STOREVALUE(group.GroupName),M_STOREVALUE(group.OwnerId),M_STOREVALUE(group.GroupNotice), M_STOREVALUE(group.PinYin),group.IsDel?@1:@0,group.isShow?@1:@0,@(group.groupType),@(group.yuLiuZiduan2),M_STOREVALUE(group.yuLiuZiduan3), M_STOREVALUE(group.GroupId)];
            
            
        } else {
           
                ret = [db executeUpdate:@"INSERT INTO GROUPINFO(GroupId, GroupIntroduction, OwnerCode, OwnerName, UserNum, CreateDate, GroupName, OwnerId, GroupNotice,messageSettingType, PinYin, IsDel,firstLetter,isShow, groupType, yuLiuZiduan1, yuLiuZiduan2, yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?,?,?,?,?,?)", M_STOREVALUE(group.GroupId), M_STOREVALUE(group.GroupIntroduction), M_STOREVALUE(group.OwnerCode), M_STOREVALUE(group.OwnerName), @(group.UserNum), M_STOREVALUE(group.GroupCreateDate),M_STOREVALUE(group.GroupName),M_STOREVALUE(group.OwnerId),M_STOREVALUE(group.GroupNotice),@(group.messageSettingType),M_STOREVALUE(group.PinYin),group.IsDel?@1:@0,M_STOREVALUE(group.firstLetter),group.isShow?@1:@0,@(group.groupType),M_STOREVALUE(group.yuLiuZiduan1),@(group.yuLiuZiduan2),M_STOREVALUE(group.yuLiuZiduan3)];
        }

    }];

    
    return ret;
}

- (BOOL)updateGroup:(CGroupModel *)group
{
    if(group == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE GROUPINFO SET GroupIntroduction = ?, OwnerCode = ?, OwnerName = ?, UserNum = ?, CreateDate = ?, GroupName = ?, OwnerId = ?, GroupNotice = ?,messageSettingType = ?, PinYin = ?,IsDel = ?,isShow = ?,groupType = ?,yuLiuZiduan1 = ?,yuLiuZiduan2 = ?,yuLiuZiduan3 = ? where GroupId = ? ", M_STOREVALUE(group.GroupIntroduction), M_STOREVALUE(group.OwnerCode), M_STOREVALUE(group.OwnerName), @(group.UserNum), M_STOREVALUE(group.GroupCreateDate),M_STOREVALUE(group.GroupName),M_STOREVALUE(group.OwnerId),M_STOREVALUE(group.GroupNotice),@(group.messageSettingType), M_STOREVALUE(group.PinYin),group.IsDel?@1:@0,group.isShow?@1:@0,@(group.groupType),M_STOREVALUE(group.yuLiuZiduan1),@(group.yuLiuZiduan2),M_STOREVALUE(group.yuLiuZiduan3), M_STOREVALUE(group.GroupId)];
    }];
    return ret;

}
//批量插入群
-(BOOL)batchInsertGroup:(NSArray *)groupList
{
    if(groupList == nil || [groupList count] <= 0)
    {
        return NO;
    }
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(CGroupModel *group in groupList)
        {
            if (!M_ISSTRNULL(group.GroupId)) {
                 ret = [db executeUpdate:@"INSERT INTO GROUPINFO(GroupId, GroupIntroduction, OwnerCode, OwnerName, UserNum, CreateDate, GroupName, OwnerId, GroupNotice,messageSettingType, PinYin, IsDel,firstLetter,isShow, groupType, yuLiuZiduan1, yuLiuZiduan2, yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?,?,?,?,?,?)", M_STOREVALUE(group.GroupId), M_STOREVALUE(group.GroupIntroduction), M_STOREVALUE(group.OwnerCode), M_STOREVALUE(group.OwnerName), @(group.UserNum), M_STOREVALUE(group.GroupCreateDate),M_STOREVALUE(group.GroupName),M_STOREVALUE(group.OwnerId),M_STOREVALUE(group.GroupNotice),@(group.messageSettingType),M_STOREVALUE(group.PinYin),group.IsDel?@1:@0,M_STOREVALUE(group.firstLetter),group.isShow?@1:@0,@(group.groupType),M_STOREVALUE(group.yuLiuZiduan1),@(group.yuLiuZiduan2),M_STOREVALUE(group.yuLiuZiduan3)];
                if(!ret)
                {
                    *rollback = YES;
                    break;
                }

            }
        }
    }];
    return ret;
}
//删除群
-(BOOL)delGroup:(CGroupModel *)group
{
    
    if(group == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"delete from GROUPINFO where identifer = ?", @(group.GroupIndex)];
    }];
    return ret;
}

- (BOOL)delAllGroup
{
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"delete from GROUPINFO"];
    }];
    return ret;

}
//加载群
-(CGroupModel *)loadGroup:(NSString *)groupId
{
    if(M_ISSTRNULL(groupId))
    {
        return nil;
    }
    NSString *sql = nil;
    __block CGroupModel *group = nil;
    sql = [NSString stringWithFormat:@"select * from GROUPINFO where  GroupId = \'%@\'", groupId ];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            group = [[CGroupModel alloc] init];
            group.GroupIndex = [result longLongIntForColumn:@"identifer"];
            group.GroupId = [result stringForColumn:@"GroupId"];
            group.GroupIntroduction = [result stringForColumn:@"GroupIntroduction"];
            group.OwnerCode = [result stringForColumn:@"OwnerCode"];
            group.OwnerName = [result stringForColumn:@"OwnerName"];
            group.UserNum = [result intForColumn:@"UserNum"];
            group.GroupCreateDate = [result stringForColumn:@"CreateDate"];
            group.GroupName = [result stringForColumn:@"GroupName"];
            group.OwnerId = [result stringForColumn:@"OwnerId"];
            group.IsDel = [result boolForColumn:@"IsDel"];
            group.GroupNotice = [result stringForColumn:@"GroupNotice"];
            group.messageSettingType = [result intForColumn:@"messageSettingType"];
            group.PinYin = [result stringForColumn:@"PinYin"];
            group.groupType = [result intForColumn:@"groupType"];
            group.isShow = [result boolForColumn:@"isShow"];
            
            group.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            group.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            group.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

        }
        [result close];
    }];
    return group;
}

- (NSArray *)searchGroupWithSeachStr:(NSString *)searchStr withGroupType:(NSInteger)groupType
{
    NSString *sql = nil;
    __block NSMutableArray *contactList = [NSMutableArray arrayWithCapacity:10];
    if (searchStr.length <= 0) {
        return nil;
    }
    if (groupType == 2) {
      sql = [NSString stringWithFormat:@"select * from GROUPINFO where IsDel = 0 AND (GroupName LIKE '%%%@%%' OR PinYin LIKE '%%%@%%' OR firstLetter LIKE '%%%@%%')",searchStr,searchStr,searchStr];
    } else {
        sql = [NSString stringWithFormat:@"select * from GROUPINFO where IsDel = 0 AND isShow = 1 AND groupType = \'%@\' AND (GroupName LIKE '%%%@%%' OR PinYin LIKE '%%%@%%' OR firstLetter LIKE '%%%@%%')",@(groupType),searchStr,searchStr,searchStr];
    }
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
          CGroupModel *group = [[CGroupModel alloc] init];
            group.GroupIndex = [result longLongIntForColumn:@"identifer"];
            group.GroupId = [result stringForColumn:@"GroupId"];
            group.GroupIntroduction = [result stringForColumn:@"GroupIntroduction"];
            group.OwnerCode = [result stringForColumn:@"OwnerCode"];
            group.OwnerName = [result stringForColumn:@"OwnerName"];
            group.UserNum = [result intForColumn:@"UserNum"];
            group.GroupCreateDate = [result stringForColumn:@"CreateDate"];
            group.GroupName = [result stringForColumn:@"GroupName"];
            group.OwnerId = [result stringForColumn:@"OwnerId"];
            group.IsDel = [result boolForColumn:@"IsDel"];
            group.GroupNotice = [result stringForColumn:@"GroupNotice"];
            group.messageSettingType = [result intForColumn:@"messageSettingType"];
            group.PinYin = [result stringForColumn:@"PinYin"];
            group.groupType = [result intForColumn:@"groupType"];
            group.isShow = [result boolForColumn:@"isShow"];
            
            group.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            group.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            group.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            [contactList addObject:group];
            group = nil;
        }
        [result close];
    }];
    if (contactList.count > 0) {
        return contactList;
    } else {
        
        return nil;
    }
    
}

-(NSArray *)loadAllGroupAndDiscussion
{
    
    NSString *sql = nil;
    __block NSMutableArray *array = [NSMutableArray arrayWithCapacity:5];
    sql = [NSString stringWithFormat:@"select * from GROUPINFO where IsDel = 0" ];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CGroupModel *group = [[CGroupModel alloc] init];
            group.GroupIndex = [result longLongIntForColumn:@"identifer"];
            group.GroupId = [result stringForColumn:@"GroupId"];
            group.GroupIntroduction = [result stringForColumn:@"GroupIntroduction"];
            group.OwnerCode = [result stringForColumn:@"OwnerCode"];
            group.OwnerName = [result stringForColumn:@"OwnerName"];
            group.UserNum = [result intForColumn:@"UserNum"];
            group.GroupCreateDate = [result stringForColumn:@"CreateDate"];
            group.GroupName = [result stringForColumn:@"GroupName"];
            group.OwnerId = [result stringForColumn:@"OwnerId"];
            group.IsDel = [result boolForColumn:@"IsDel"];
            group.GroupNotice = [result stringForColumn:@"GroupNotice"];
            group.messageSettingType = [result intForColumn:@"messageSettingType"];
            group.PinYin = [result stringForColumn:@"PinYin"];
            group.groupType = [result intForColumn:@"groupType"];
            group.isShow = [result boolForColumn:@"isShow"];
            
            group.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            group.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            group.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            
            [array addObject:group];
            group = nil;
        }
        [result close];
    }];
    return array;
}


//加载所有群
-(NSArray *)loadAllGroup
{

    NSString *sql = nil;
    __block NSMutableArray *array = [NSMutableArray arrayWithCapacity:5];
    sql = [NSString stringWithFormat:@"select * from GROUPINFO where IsDel = 0 AND groupType = 0" ];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CGroupModel *group = [[CGroupModel alloc] init];
            group.GroupIndex = [result longLongIntForColumn:@"identifer"];
            group.GroupId = [result stringForColumn:@"GroupId"];
            group.GroupIntroduction = [result stringForColumn:@"GroupIntroduction"];
            group.OwnerCode = [result stringForColumn:@"OwnerCode"];
            group.OwnerName = [result stringForColumn:@"OwnerName"];
            group.UserNum = [result intForColumn:@"UserNum"];
            group.GroupCreateDate = [result stringForColumn:@"CreateDate"];
            group.GroupName = [result stringForColumn:@"GroupName"];
            group.OwnerId = [result stringForColumn:@"OwnerId"];
            group.IsDel = [result boolForColumn:@"IsDel"];
            group.GroupNotice = [result stringForColumn:@"GroupNotice"];
            group.messageSettingType = [result intForColumn:@"messageSettingType"];
            group.PinYin = [result stringForColumn:@"PinYin"];
            group.groupType = [result intForColumn:@"groupType"];
            group.isShow = [result boolForColumn:@"isShow"];

            group.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            group.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            group.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            [array addObject:group];
            group = nil;
        }
        [result close];
    }];
    return array;
}

//加载所有讨论组
- (NSArray *)loadAllDiscussList
{
    NSString *sql = nil;
    __block NSMutableArray *array = [NSMutableArray arrayWithCapacity:5];
    sql = [NSString stringWithFormat:@"select * from GROUPINFO where IsDel = 0 AND groupType = 1" ];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CGroupModel *group = [[CGroupModel alloc] init];
            group.GroupIndex = [result longLongIntForColumn:@"identifer"];
            group.GroupId = [result stringForColumn:@"GroupId"];
            group.GroupIntroduction = [result stringForColumn:@"GroupIntroduction"];
            group.OwnerCode = [result stringForColumn:@"OwnerCode"];
            group.OwnerName = [result stringForColumn:@"OwnerName"];
            group.UserNum = [result intForColumn:@"UserNum"];
            group.GroupCreateDate = [result stringForColumn:@"CreateDate"];
            group.GroupName = [result stringForColumn:@"GroupName"];
            group.OwnerId = [result stringForColumn:@"OwnerId"];
            group.IsDel = [result boolForColumn:@"IsDel"];
            group.GroupNotice = [result stringForColumn:@"GroupNotice"];
            group.messageSettingType = [result intForColumn:@"messageSettingType"];
            group.PinYin = [result stringForColumn:@"PinYin"];
            group.groupType = [result intForColumn:@"groupType"];
            group.isShow = [result boolForColumn:@"isShow"];
            
            group.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            group.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            group.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            
            [array addObject:group];
            group = nil;
        }
        [result close];
    }];
    return array;

}
- (BOOL)updateGroupIsDelWithGroupId:(NSString *)groupId IsDel:(BOOL)isDel
{
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE GROUPINFO SET isDel = ? where GroupId = ?",isDel?@1:@0,groupId];
        
    }];
    return ret;

}

- (BOOL)delGroupWithGroupId:(NSString *)groupId
{
    if(groupId == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"delete from GROUPINFO where GroupId = ?", groupId];
    }];
    return ret;
    
}

- (BOOL)isAvalableGroupWithGroupId:(NSString *)GroupId
{
    if(M_ISSTRNULL(GroupId))
    {
        return nil;
    }
    NSString *sql = nil;
    __block BOOL IsDel = YES;
    sql = [NSString stringWithFormat:@"select IsDel from GROUPINFO where  GroupId = \'%@\'", GroupId];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            IsDel = [result boolForColumn:@"IsDel"];
        }
        [result close];
    }];
    return !IsDel;

}
@end
